package com.dfbz.controller;

import com.dfbz.model.User;
import com.dfbz.service.Impl.UserServiceImpl;
import com.dfbz.service.UserService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

@WebServlet("/user/login")
public class LoginController extends HttpServlet {

    private UserService userService = new UserServiceImpl();

    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String username = req.getParameter("uname");
        String password = req.getParameter("pwd");
        User user = userService.findByName(username);
    //登录模块:
        // 1.先判断用户名是否为空
        // 2.判断数据库中是否有该用户
        // 3.再判断该用户的密码是否与数据库的密码一致
     if (null!=user){
         if (password.equals(user.getPassword())){
             resp.sendRedirect(req.getContextPath()+"/assets/html/home.html");
             return;
         }else {
             req.getSession().setAttribute("loginMsg","密码不正确");
             resp.sendRedirect(req.getContextPath()+"/index.html");

         }
     }else {
         req.getSession().setAttribute("loginMsg","找不到该用户");
         resp.sendRedirect(req.getContextPath()+"/index.html");
     }


    }
}
